package leetcode.listnode;

import leetcode.ListNode;

import java.util.List;

/**
 * @author Cheng Jun
 * Description: 给你单链表的头指针 head 和两个整数  left 和 right ，其中  left <= right 。
 * 请你反转从位置 left 到位置 right 的链表节点，返回 反转后的链表 。
 * <p>
 * 来源：力扣（LeetCode）
 * 链接：https://leetcode.cn/problems/reverse-linked-list-ii
 * 著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
 * @version 1.0
 * @date 2022/7/19 22:36
 */
public class reverseBetween {

    public static void main(String[] args) {

    }

    static ListNode reverseBetween(ListNode head, int left, int right) {
        // [1,2,3,4,5], left = 2, right = 4
        // [1,4,3,2,5]
        ListNode dummy = new ListNode(-1, head);
        ListNode pointer = dummy;
        ListNode preNode = null;
        ListNode betweenNode = null;
        ListNode betweenLastNode = null;
        int i = 0;
        int times = 0;
        while (pointer != null) {
            // 先找到前置节点, 即 left - 1节点
            if (preNode == null) {
                if (i == left - 1) {
                    preNode = pointer;
                } else {
                    i++;
                    pointer = pointer.next;
                }
            } else {
                if (times <= (right - left)) {
                    betweenNode = new ListNode(pointer.val, betweenNode);
                    if (times == 0) {
                        betweenLastNode = betweenNode;
                    }
                    times++;
                    pointer = pointer.next;
                } else {
                    betweenLastNode.next = pointer;
                    preNode.next = betweenNode;
                    break;
                }
            }
        }
        return dummy.next;
    }

    // TODO: 2022/7/20
    static ListNode reverseBetween1(ListNode head, int left, int right) {
        // [1,2,3,4,5], left = 2, right = 4
        // [1,4,3,2,5]
        ListNode dummy = new ListNode(-1, head);
        ListNode pointer = dummy;
        ListNode preNode = null;
        ListNode leftNoe = null;
        int i = 0;
        while (preNode == null) {
            if(i == left -1){
                preNode = pointer;
                leftNoe = pointer.next;
            }else {
                i++;
                pointer = pointer.next;
            }
        }
        i = 0;
        while (i < right -left){
            
        }


        return dummy.next;
    }
}
